﻿using System.Globalization;

namespace NetEasyMusic.Lib.Operations;

public static class NumOpera
{
    public static double ToTime(this int i)
    {
        var d = i / 1000.0;
        var s = d.ToString(CultureInfo.InvariantCulture).Split(".");
        if (s.Length < 2)
            return d;
        var a = s[1].Length > 2 ? s[1][..2] : s[1];
        return double.Parse($"{s[0]}.{a}");
    }

    public static string ToTimeString(this int i)
    {
        i /= 1000;
        var s = "";
        while (true)
        {
            if (string.IsNullOrEmpty(s))
            {
                var r = i % 60;
                s = r < 10 ? $"0{r}" : r.ToString();
            }
            else
            {
                s = $"{i % 60}:" + s;
            }

            i /= 60;
            if (i < 1)
                break;
        }

        return !s.Contains(':') ? $"0:{s}" : s;
    }

    public static double SizeToGB(this long l)
    {
        l /= 1024 * 1024;
        return Math.Round(l / 1024.0);
    }
}